package com.innjia.sys.entity;

import com.alibaba.druid.util.StringUtils;
import com.innjia.base.exception.MyException;
import com.innjia.base.utils.DateUtils;
import com.innjia.base.utils.Utils;
import com.innjia.base.utils.ValidatorUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 房源信息模型
 * 
 * @author innjia
 * @email huangxianyuan@gmail.com
 * @date 2017-11-13 19:07:07
 */
public class HouseEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	
	//记录主键
	private String id;
	//创建记录时间
	private Date createTime;
	//记录状态
	private String status = "1";  //1 可用,  0 删除
	//更新记录时间
	private Date updateTime;
	//申请状态
	private String applyStatus;
	//出租状态
	private String rentStatus;
	//序列号
	private String serialNum;
	//城市
	private String cityName;
	//区(县)
	private String districtName;
	//地址
	private String address;
	//小区
	private String vilage;
	//栋
	private String blockNo;
	//单元
	private String unitNo;
	//室
	private String roomNo;
	//楼层
	private String floorNo;
	//门号
	private String door;
	//大门ID
	private String parentId;
	//户型
	private String houseType;
	//面积
	private String buildingArea;
	//房间朝向
	private String direct;
	//租房类型
	private String rentType;
	//租金
	private String rentalPrice;
	//付款方式
	private String payMode;
	//房间配套
	private String hardware;
	//房源来源
	private String source;
	//房源备注
	private String remark;
	//房东ID
	private String ownerId;
	//卧室数量
	private String bedroomCnt;
	//客厅数量
	private String parlourCnt;
	//卫生间数量
	private String toiletCnt;
	//创建者ID
	private String creatorId;
	//审核人ID
	private String checkerId;
	//版本
	private Long version;
	//房屋所有权类型(公租/个人)
	private String ownershipType = "个人";

	private String housePropertyCertificateId; //房源产权证书ID
	private String txHash; //区块链入链hash
	private String housePropertySerialCode; //产权编码

	private List<HouseImageEntity> houseImages;
	private List<HouseAuthImageEntity> houseAuthImages;
	private List<HouseEntity> children;
	private UserLandlordEntity landlord;
	private List<HouseOperationLogEntity> houseOperationLogs;
	private List<ContractInfoEntity> contractInfos;

	private String hardwaresClass; //房源配套设施的 class 描述, 用于APP端渲染页面图标
	private String ownerPhone; //房东手机
	
	/**
	 * 设置：记录主键
	 */
	public void setId(String id) {
		this.id = id;
	}
	/**
	 * 获取：记录主键
	 */
	public String getId() {
		return id;
	}
	/**
	 * 设置：创建记录时间
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	/**
	 * 获取：创建记录时间
	 */
	public Date getCreateTime() {
		return createTime;
	}
	/**
	 * 设置：记录状态
	 */
	public void setStatus(String status) {
		this.status = status;
	}
	/**
	 * 获取：记录状态
	 */
	public String getStatus() {
		return status;
	}
	/**
	 * 设置：更新记录时间
	 */
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	/**
	 * 获取：更新记录时间
	 */
	public Date getUpdateTime() {
		return updateTime;
	}
	/**
	 * 设置：申请状态
	 */
	public void setApplyStatus(String applyStatus) {
		this.applyStatus = applyStatus;
	}
	/**
	 * 获取：申请状态
	 */
	public String getApplyStatus() {
		return applyStatus;
	}
	/**
	 * 设置：序列号
	 */
	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}
	/**
	 * 获取：序列号
	 */
	public String getSerialNum() {
		return serialNum;
	}
	/**
	 * 设置：城市
	 */
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	/**
	 * 获取：城市
	 */
	public String getCityName() {
		return cityName;
	}
	/**
	 * 设置：区(县)
	 */
	public void setDistrictName(String districtName) {
		this.districtName = districtName;
	}
	/**
	 * 获取：区(县)
	 */
	public String getDistrictName() {
		return districtName;
	}
	/**
	 * 设置：地址
	 */
	public void setAddress(String address) {
		this.address = address;
	}
	/**
	 * 获取：地址
	 */
	public String getAddress() {
		return address;
	}
	/**
	 * 设置：栋
	 */
	public void setBlockNo(String blockNo) {
		this.blockNo = blockNo;
	}
	/**
	 * 获取：栋
	 */
	public String getBlockNo() {
		return blockNo;
	}
	/**
	 * 设置：单元
	 */
	public void setUnitNo(String unitNo) {
		this.unitNo = unitNo;
	}
	/**
	 * 获取：单元
	 */
	public String getUnitNo() {
		return unitNo;
	}
	/**
	 * 设置：室
	 */
	public void setRoomNo(String roomNo) {
		this.roomNo = roomNo;
	}
	/**
	 * 获取：室
	 */
	public String getRoomNo() {
		return roomNo;
	}
	/**
	 * 设置：楼层
	 */
	public void setFloorNo(String floorNo) {
		this.floorNo = floorNo;
	}
	/**
	 * 获取：楼层
	 */
	public String getFloorNo() {
		return floorNo;
	}
	/**
	 * 设置：门号
	 */
	public void setDoor(String door) {
		this.door = door;
	}
	/**
	 * 获取：门号
	 */
	public String getDoor() {
		return door;
	}
	/**
	 * 设置：大门ID
	 */
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}
	/**
	 * 获取：大门ID
	 */
	public String getParentId() {
		return parentId;
	}
	/**
	 * 设置：户型
	 */
	public void setHouseType(String houseType) {
		this.houseType = houseType;
	}
	/**
	 * 获取：户型
	 */
	public String getHouseType() {
		return houseType;
	}
	/**
	 * 设置：面积
	 */
	public void setBuildingArea(String buildingArea) {
		this.buildingArea = buildingArea;
	}
	/**
	 * 获取：面积
	 */
	public String getBuildingArea() {
		return buildingArea;
	}
	/**
	 * 设置：房间朝向
	 */
	public void setDirect(String direct) {
		this.direct = direct;
	}
	/**
	 * 获取：房间朝向
	 */
	public String getDirect() {
		return direct;
	}
	/**
	 * 设置：租房类型
	 */
	public void setRentType(String rentType) {
		this.rentType = rentType;
	}
	/**
	 * 获取：租房类型
	 */
	public String getRentType() {
		return rentType;
	}
	/**
	 * 设置：租金
	 */
	public void setRentalPrice(String rentalPrice) {
		this.rentalPrice = rentalPrice;
	}
	/**
	 * 获取：租金
	 */
	public String getRentalPrice() {
		return rentalPrice;
	}
	/**
	 * 设置：付款方式
	 */
	public void setPayMode(String payMode) {
		this.payMode = payMode;
	}
	/**
	 * 获取：付款方式
	 */
	public String getPayMode() {
		return payMode;
	}
	/**
	 * 设置：房间配套
	 */
	public void setHardware(String hardware) {
		this.hardware = hardware;
	}
	/**
	 * 获取：房间配套
	 */
	public String getHardware() {
		return hardware;
	}
	/**
	 * 设置：房源来源
	 */
	public void setSource(String source) {
		this.source = source;
	}
	/**
	 * 获取：房源来源
	 */
	public String getSource() {
		return source;
	}
	/**
	 * 设置：房源备注
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}
	/**
	 * 获取：房源备注
	 */
	public String getRemark() {
		return remark;
	}
	/**
	 * 设置：房东ID
	 */
	public void setOwnerId(String ownerId) {
		this.ownerId = ownerId;
	}
	/**
	 * 获取：房东ID
	 */
	public String getOwnerId() {
		return ownerId;
	}
	/**
	 * 设置：卧室数量
	 */
	public void setBedroomCnt(String bedroomCnt) {
		this.bedroomCnt = bedroomCnt;
	}
	/**
	 * 获取：卧室数量
	 */
	public String getBedroomCnt() {
		return bedroomCnt;
	}
	/**
	 * 设置：客厅数量
	 */
	public void setParlourCnt(String parlourCnt) {
		this.parlourCnt = parlourCnt;
	}
	/**
	 * 获取：客厅数量
	 */
	public String getParlourCnt() {
		return parlourCnt;
	}
	/**
	 * 设置：卫生间数量
	 */
	public void setToiletCnt(String toiletCnt) {
		this.toiletCnt = toiletCnt;
	}
	/**
	 * 获取：卫生间数量
	 */
	public String getToiletCnt() {
		return toiletCnt;
	}
	/**
	 * 设置：创建者ID
	 */
	public void setCreatorId(String creatorId) {
		this.creatorId = creatorId;
	}
	/**
	 * 获取：创建者ID
	 */
	public String getCreatorId() {
		return creatorId;
	}
	/**
	 * 设置：审核人ID
	 */
	public void setCheckerId(String checkerId) {
		this.checkerId = checkerId;
	}
	/**
	 * 获取：审核人ID
	 */
	public String getCheckerId() {
		return checkerId;
	}
	/**
	 * 设置：版本
	 */
	public void setVersion(Long version) {
		this.version = version;
	}
	/**
	 * 获取：版本
	 */
	public Long getVersion() {
		return version;
	}

	public String getVilage() {
		return vilage;
	}

	public void setVilage(String vilage) {
		this.vilage = vilage;
	}

	public String getRentStatus() {
		return rentStatus;
	}

	public void setRentStatus(String rentStatus) {
		this.rentStatus = rentStatus;
	}

	public List<HouseImageEntity> getHouseImages() {
		return houseImages;
	}

	public void setHouseImages(List<HouseImageEntity> houseImages) {
		this.houseImages = houseImages;
	}

	public List<HouseAuthImageEntity> getHouseAuthImages() {
		return houseAuthImages;
	}

	public void setHouseAuthImages(List<HouseAuthImageEntity> houseAuthImages) {
		this.houseAuthImages = houseAuthImages;
	}

	public List<HouseEntity> getChildren() {
		return children;
	}

	public void setChildren(List<HouseEntity> children) {
		this.children = children;
	}

	public String getHardwaresClass() {
		return hardwaresClass;
	}

	public void setHardwaresClass(String hardwaresClass) {
		this.hardwaresClass = hardwaresClass;
	}

	public String getOwnerPhone() {
		if(StringUtils.isEmpty(ownerPhone) && landlord!=null) {
			this.ownerPhone = landlord.getPhone();
		}
		return ownerPhone;
	}

	public void setOwnerPhone(String ownerPhone) {
		this.ownerPhone = ownerPhone;
	}

	public String getOwnershipType() {
		return ownershipType;
	}

	public void setOwnershipType(String ownershipType) {
		this.ownershipType = ownershipType;
	}

	public String getHousePropertyCertificateId() {
		return housePropertyCertificateId;
	}

	public void setHousePropertyCertificateId(String housePropertyCertificateId) {
		this.housePropertyCertificateId = housePropertyCertificateId;
	}

	public String getTxHash() {
		return txHash;
	}

	public void setTxHash(String txHash) {
		this.txHash = txHash;
	}

	public String getHousePropertySerialCode() {
		return housePropertySerialCode;
	}

	public void setHousePropertySerialCode(String housePropertySerialCode) {
		this.housePropertySerialCode = housePropertySerialCode;
	}

	public UserLandlordEntity getLandlord() {
		return landlord;
	}
	public void setLandlord(UserLandlordEntity landlord) {
		this.landlord = landlord;
	}
	public List<HouseOperationLogEntity> getHouseOperationLogs() {
		return houseOperationLogs;
	}
	public void setHouseOperationLogs(List<HouseOperationLogEntity> houseOperationLogs) {
		this.houseOperationLogs = houseOperationLogs;
	}

	public List<ContractInfoEntity> getContractInfos() {
		return contractInfos;
	}
	public void setContractInfos(List<ContractInfoEntity> contractInfos) {
		this.contractInfos = contractInfos;
	}
	//填充 '户型' 信息
	public void initHouseType(){
		if(ValidatorUtils.isNotEmpty(houseType)){
			return;
		}

		String roomCnt = ValidatorUtils.isEmpty(this.bedroomCnt) ? "0" : this.bedroomCnt;
		String tingCnt = ValidatorUtils.isEmpty(this.parlourCnt) ? "0" : this.parlourCnt;
		String toiletCnt = ValidatorUtils.isEmpty(this.toiletCnt) ? "0" : this.toiletCnt;

		this.houseType = roomCnt + "室" + tingCnt + "厅" + toiletCnt + "卫";
	}

	//快速创建子房间
	public HouseEntity fastGenChildRoom(){
		HouseEntity child = new HouseEntity();

		try {
			PropertyUtils.copyProperties(child, this);
		}  catch (Exception e) {
			e.printStackTrace();
			throw new MyException("创建子房间出错");
		}

		child.setId(Utils.uuid());
		child.setParentId(this.getId());
		child.setSerialNum(null);
		return child;
	}

	//处理字段信息为空
	public void handleNullProperty(){
		//这些字段作为数据库联合唯一约束, 任意一个为 null 时，将不起约束作用
		//cityName`,`districtName`,`address`,`vilage`,`blockNo`,`unitNo`,`roomNo`,`floorNo`,`door`

		if(districtName == null){
			districtName = "";
		}
		if(address == null){
			address = "";
		}
		if(vilage == null){
			vilage = "";
		}
		if(blockNo == null){
			blockNo = "";
		}
		if(unitNo == null){
			unitNo = "";
		}
		if(floorNo == null){
			floorNo = "";
		}
		if(roomNo == null){
			roomNo = "";
		}
		if(door == null){
			door = "";
		}
	}
	
	/**
	 * 获取房源详细地址
	 * @return
	 * string
	 */
	public String hosueDetailAddress() {
		StringBuffer sb = new StringBuffer();
		sb.append(this.cityName);
		sb.append(this.districtName);
		sb.append(this.address);
		sb.append(this.vilage);
		if(!StringUtils.isEmpty(this.blockNo)) {
			sb.append(this.blockNo + "栋");
		}
		if(!StringUtils.isEmpty(this.unitNo)) {
			sb.append(this.unitNo + "单元");
		}
		if(!StringUtils.isEmpty(this.roomNo)) {
			sb.append(this.roomNo + "室");
		}
		return sb.toString();
	}

	private String basicMsg;
	public void setBasicMsg(String basicMsg){
		this.basicMsg = basicMsg;
	}
	/**
	 * 获取房源基本信息:1室1厅1卫 44㎡
	 */
	public String getBasicMsg() {
		
		return (StringUtils.isEmpty(houseType) ? "0室0厅0卫" : houseType) + "  " + (StringUtils.isEmpty(buildingArea) ? "0" : buildingArea) + "㎡";
	}
	
	/*
	 * 合同信息转Map，字段名为name,字段值为value
	 */
	public Map<String, Object> getMapDatas(Map<String, Object> map) throws Exception {
		if(map == null) {
			map = new HashMap<String, Object>(64);
		}
		Class<? extends HouseEntity> clazz = this.getClass();
		Field[] fields = clazz.getDeclaredFields();
		if(fields != null && fields.length > 0) {
			for(Field field : fields) {
				String fieldName = field.getName();
				if(fieldName.equals("serialVersionUID")) {
					continue;
				}
				String fieldType = field.getType().getName();
				if ("java.util.Date".equals(fieldType)) {
					map.put(fieldName,  DateUtils.format((Date)field.get(this), "yyyy-MM-dd"));
				} else {
					map.put(fieldName, field.get(this));
				}
			}
		}
		return map;
	}

	public Map<String, String> getMapDatas1(Map<String, String> map) throws Exception {
		if(map == null) {
			map = new HashMap<String, String>();
		}
		Class<? extends HouseEntity> clazz = this.getClass();
		Field[] fields = clazz.getDeclaredFields();
		if(fields != null && fields.length > 0) {
			for(Field field : fields) {
				String fieldName = field.getName();
				if(fieldName.equals("serialVersionUID")) {
					continue;
				}
				String fieldType = field.getType().getName();
				if ("java.util.Date".equals(fieldType)) {
					Date date = (Date) field.get(this);
					if (date !=null){
						map.put(fieldName,  date.getTime()+"");
					}else {
					map.put(fieldName,  "");
					}

				} else {
					map.put(fieldName, field.get(this)==null? "":field.get(this).toString());
				}
			}
		}
		return map;
	}

}
